import sys
import gc

# 创建两个对象，互相引用
class Node:
    def __init__(self):
        self.next = None

a = Node()
b = Node()
a.next = b
b.next = a

# 查看引用计数
print("Reference count of a:", sys.getrefcount(a))  # 输出会比预期多1，因为getrefcount也会增加引用计数
print("Reference count of b:", sys.getrefcount(b))

'''
输出:
Reference count of a: 3
Reference count of b: 3
'''

# 手动断开引用
a.next = None
b.next = None

# 启用垃圾回收
gc.collect()

# 查看对象是否被回收
print("Is a still alive?", 'a' in globals())  # 输出: True
print("Is b still alive?", 'b' in globals())  # 输出: True

print("Reference count of a:", sys.getrefcount(a))  # 输出: 2
print("Reference count of b:", sys.getrefcount(b))  # 输出: 2